<?php
namespace Admin\Controller;

class QrcodeController extends AdminController {
	public function gen(){
		$this->assign('cates', R('Common/getQrcodeCates'));
		$this->assign('schoolList', R('Common/getSchoolList'));
		$this->display();
	}
	
    public function doGen(){
    	$map = array();
    	$cateId = I('qrcode_category_id');
    	$schoolId = I('school_id');
    	$title = I('title');
    	if ($cateId > 0) {
    		$map['qrcode_category_id'] = $cateId;
    	}
    	if ($schoolId > 0) {
    		$map['school_id'] = $schoolId;
    	}
    	if (!empty($title)) {
    		$map['title'] = $title;
    	}
    	
    	vendor('phpqrcode'); //$qrcode = new \QRcode();
    	$qrcodeList = D('Qrcode')->field('id,school_id')->where($map)->select();
    	foreach ($qrcodeList as $item){
    		$dirName = './Data/qrcode/' . $item['school_id']; //按学校ID分目录，不需要再按分类ID
    		if (!is_dir($dirName)) {
    			mkdir($dirName);
    		}
    		$filename 	= $dirName . '/' . $item['id'] . '.png';
    		$genTime 	= time();
    		$data 		= $item['id'] . '@' . $genTime; //加密后写入
    		\QRcode::png($data, $filename, QR_ECLEVEL_L, 15, 1);
    		D('Qrcode')->save(array('id' => $item['id'], 'gen_time' => $genTime));
    		/* echo $item['id'] . ' success!<br/>';
    		ob_flush();
    		flush(); */
    	}
    	$this->success('生成成功');
    }
    
    public function reGen(){
    	//
    }
    
    public function del(){
    	//
    }

}